package com.yic.module.qms.controller.admin.inspectschemeentry;

import cn.hutool.core.util.ObjectUtil;
import com.yic.module.base.api.factory.FactoryApi;
import com.yic.module.base.api.factory.dto.FactoryRespDTO;
import com.yic.module.base.api.unit.UnitApi;
import com.yic.module.base.api.unit.dto.UnitRespDTO;
import com.yic.module.mes.api.process.ProcessApi;
import com.yic.module.mes.api.process.dto.ProcessRespDTO;
import com.yic.module.qms.dal.dataobject.inspectentry.InspectEntryDO;
import com.yic.module.qms.service.inspectentry.InspectEntryService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.yic.framework.common.pojo.PageResult;
import com.yic.framework.common.pojo.CommonResult;
import static com.yic.framework.common.pojo.CommonResult.success;

import com.yic.framework.excel.core.util.ExcelUtils;

import com.yic.framework.operatelog.core.annotations.OperateLog;
import static com.yic.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.yic.module.qms.controller.admin.inspectschemeentry.vo.*;
import com.yic.module.qms.dal.dataobject.inspectschemeentry.InspectSchemeEntryDO;
import com.yic.module.qms.convert.inspectschemeentry.InspectSchemeEntryConvert;
import com.yic.module.qms.service.inspectschemeentry.InspectSchemeEntryService;

@Tag(name = "管理后台 - 检验方案检验项关联")
@RestController
@RequestMapping("/qms/inspect-scheme-entry")
@Validated
public class InspectSchemeEntryController {

    @Resource
    private InspectSchemeEntryService inspectSchemeEntryService;
    @Resource
    private InspectEntryService inspectEntryService;
    @Resource
    private FactoryApi factoryApi;
    @Resource
    private UnitApi unitApi;
    @Resource
    private ProcessApi processApi;

    @PostMapping("/create")
    @Operation(summary = "创建检验方案检验项关联")
    @PreAuthorize("@ss.hasPermission('qms:inspect-scheme-entry:create')")
    public CommonResult<Long> createInspectSchemeEntry(@Valid @RequestBody InspectSchemeEntryCreateReqVO createReqVO) {
        return success(inspectSchemeEntryService.createInspectSchemeEntry(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新检验方案检验项关联")
    @PreAuthorize("@ss.hasPermission('qms:inspect-scheme-entry:update')")
    public CommonResult<Boolean> updateInspectSchemeEntry(@Valid @RequestBody InspectSchemeEntryUpdateReqVO updateReqVO) {
        inspectSchemeEntryService.updateInspectSchemeEntry(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除检验方案检验项关联")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('qms:inspect-scheme-entry:delete')")
    public CommonResult<Boolean> deleteInspectSchemeEntry(@RequestParam("id") Long id) {
        inspectSchemeEntryService.deleteInspectSchemeEntry(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得检验方案检验项关联")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('qms:inspect-scheme-entry:query')")
    public CommonResult<InspectSchemeEntryRespVO> getInspectSchemeEntry(@RequestParam("id") Long id) {
        InspectSchemeEntryDO inspectSchemeEntry = inspectSchemeEntryService.getInspectSchemeEntry(id);
        InspectSchemeEntryRespVO respVO = InspectSchemeEntryConvert.INSTANCE.convert(inspectSchemeEntry);
        // 封装检验项目
        InspectEntryDO inspectEntry = inspectEntryService.getInspectEntry(respVO.getEntryId());
        if (ObjectUtil.isNotEmpty(inspectEntry)) {
            respVO.setEntryName(inspectEntry.getName());
            FactoryRespDTO factory = factoryApi.getFactory(inspectEntry.getFactoryId());
            if (ObjectUtil.isNotEmpty(factory)) {
                respVO.setFactoryName(factory.getName());
            }
        }
        // 封装工艺
        ProcessRespDTO process = processApi.getProcess(respVO.getProcessId());
        if (ObjectUtil.isNotEmpty(process)) {
            respVO.setProcessName(process.getName());
        }
        return success(respVO);
    }

    @GetMapping("/list")
    @Operation(summary = "获得检验方案检验项关联列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('qms:inspect-scheme-entry:query')")
    public CommonResult<List<InspectSchemeEntryRespVO>> getInspectSchemeEntryList(@RequestParam("ids") Collection<Long> ids) {
        List<InspectSchemeEntryDO> list = inspectSchemeEntryService.getInspectSchemeEntryList(ids);
        return success(InspectSchemeEntryConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得检验方案检验项关联分页")
    @PreAuthorize("@ss.hasPermission('qms:inspect-scheme-entry:query')")
    public CommonResult<PageResult<InspectSchemeEntryRespVO>> getInspectSchemeEntryPage(@Valid InspectSchemeEntryPageReqVO pageVO) {
        PageResult<InspectSchemeEntryDO> pageResult = inspectSchemeEntryService.getInspectSchemeEntryPage(pageVO);
        PageResult<InspectSchemeEntryRespVO> resp = InspectSchemeEntryConvert.INSTANCE.convertPage(pageResult);
        resp.getList().forEach(item -> {
            // 封装检验项目
            InspectEntryDO inspectEntry = inspectEntryService.getInspectEntry(item.getEntryId());
            if (ObjectUtil.isNotEmpty(inspectEntry)) {
                item.setEntryName(inspectEntry.getName());
                FactoryRespDTO factory = factoryApi.getFactory(inspectEntry.getFactoryId());
                if (ObjectUtil.isNotEmpty(factory)) {
                    item.setFactoryName(factory.getName());
                }
            }
            // 封装单位
            UnitRespDTO unit = unitApi.getUnit(item.getUnitId());
            if (ObjectUtil.isNotEmpty(unit)) {
                item.setUnitName(unit.getName());
            }
            // 封装工艺
            ProcessRespDTO process = processApi.getProcess(item.getProcessId());
            if (ObjectUtil.isNotEmpty(process)) {
                item.setProcessName(process.getName());
            }
        });
        return success(resp);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出检验方案检验项关联 Excel")
    @PreAuthorize("@ss.hasPermission('qms:inspect-scheme-entry:export')")
    @OperateLog(type = EXPORT)
    public void exportInspectSchemeEntryExcel(@Valid InspectSchemeEntryExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<InspectSchemeEntryDO> list = inspectSchemeEntryService.getInspectSchemeEntryList(exportReqVO);
        // 导出 Excel
        List<InspectSchemeEntryExcelVO> datas = InspectSchemeEntryConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "检验方案检验项关联.xls", "数据", InspectSchemeEntryExcelVO.class, datas);
    }

}
